package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.DishService;
import com.itheima.bean.Dish;
import com.itheima.bean.Params;
import com.itheima.bean.R;
import com.itheima.dto.DishDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/dish")
@RestController
public class DishController {

    @Autowired
    private DishService dishService;

    @GetMapping("/dishDto")
    public R getDishDto(Long categoryId,int status){
        System.out.println("=============="+categoryId+"============="+status);
        List<DishDto> dishList= dishService.findUserDish(categoryId,status);
        return R.success(dishList);
    }


    @GetMapping("/list")
    public R getDish(Long categoryId){
        System.out.println("====================================="+categoryId);
        List<Dish> dishList= dishService.findDish(categoryId);
      return R.success(dishList);
    }

    @PutMapping
    public R updata(@RequestBody DishDto dishDto){

        int row=dishService.updata(dishDto);
       if (row>0){
           return R.success("修改菜品成功！");
       }return R.error("修改菜品失败！");

    }

    @GetMapping("/{params}")
    public R findPage(@PathVariable String params){
        Params params1 = JSON.parseObject(params, Params.class);
       IPage<DishDto> dtoIPage = dishService.findPage(params1);

           return R.success(dtoIPage);

    }

    @PostMapping
    public R add(@RequestBody DishDto dishDto){
        System.out.println(dishDto);
        int row = dishService.add(dishDto);

      if (row==1){
          return R.success("菜品新增成功！");
      }else {
          return R.error("菜品新增失败！");
      }
    }
}
